import { reactive, ref } from "vue"
import { useDict } from "@/dict"
export function useWar(useCommonState){
	const { useDictList } = useDict()
  const useStateWar = reactive({
	  // 模式
	  modeCategory: useDictList('warModule'),
  })
  
  const changeWarType = (item) => {
	  useCommonState.current = item
  }
  
  const getRandomNumber = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

	const useBlend = () => {
		const idx = getRandomNumber(0, 2) + 1
		if(idx === 1) { greaterFormula(idx) }
		if(idx === 2) { lessFormula(idx) }
		if(idx === 3) { equalFormula(idx) }
	}
  
  // 创建
  const useCreateWar = () => {
	return new Promise((resolve, reject) => {
		if(!useCommonState.topicNumber) {
			  wx.showToast({title:"请选择数量", icon: "none"})
			  reject()
			  return
		}
		if(!useCommonState.current) {
			  wx.showToast({title:"请选择算式模式", icon: "none"})
			  reject()
			  return
		}
		useCommonState.topicList = []
		 if(useCommonState.current.greater) {
			for(let i = 0; i < useCommonState.topicNumber; i++){
				greaterFormula(1)
			}
		 }
		 
		 if(useCommonState.current.less) {
			for(let i = 0; i < useCommonState.topicNumber; i++){
				lessFormula(2)
			}
		 }
		 
		 if(useCommonState.current.equal) {
			for(let i = 0; i < useCommonState.topicNumber; i++){
				equalFormula(3)
			}
		 }
		 
		 if(useCommonState.current.blend) {
			for(let i = 0; i < useCommonState.topicNumber; i++){
				useBlend()
			}
		 }
		 resolve()
	 })
  }
  
  // 随机值 - 大于
  const greaterFormula = (result) => {
    let start = getRandomNumber(useCommonState.rangeValue[0], useCommonState.rangeValue[1])
    start = start === 0 ? 1 : start
    const end = getRandomNumber(0, start-1)
	const obj = { start, end, result: '>', validate: null }
	useCommonState.topicList.push(obj)
  }
  // 随机值 - 小于
  const lessFormula = (result) => {
    let start = getRandomNumber(useCommonState.rangeValue[0], useCommonState.rangeValue[1])
    start = start === useCommonState.rangeValue[1] ? useCommonState.rangeValue[1] - 1 : start
    const end = getRandomNumber(start+1, useCommonState.rangeValue[1])
    const obj = { start, end, result: '<' }
	
    useCommonState.topicList.push(obj)
  }
  // 随机值 - 相等
  const equalFormula = (result) => {
    let start = getRandomNumber(useCommonState.rangeValue[0], useCommonState.rangeValue[1])
    const end = start
    const obj = { start, end, result: '=' }
    useCommonState.topicList.push(obj)
  }

  return {
	  useStateWar,
	  useCreateWar,
	  changeWarType
  }

}